package com.example.fashion.android.FourCompoents.provider;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;

import com.example.fashion.android.util.DBUtil.DataBaseHelper;

/**
 * 在内容提供者里面创建的,用于测试
 */
public class MyBookProvider extends ContentProvider {
    //查询Table1表中所有数据
    public static final int BOOK_DIR = 0;
    //查询Table1表中的单条数据
    public static final int BOOK_ITEM = 1;
    //查询Table2表中的所有数据
    public static final int CATEGORY_DIR = 2;
    //查询Table2表中的单条数据
    public static final int CATEGORY_ITEM = 3;

    // authority：主机名（我们可以自己定义，随意写，当使用内容解析者时会使用到）
    public static final String AUTHORITY = "com.example.fashion.android.FourCompoents.provider";

    //数据库名
    private static final String DATABASE_NAME = "BookStore.db";

    //数据库版本号
    private static final int DATABASE_VERSION = 1;

    // UriMatcher是一个工具类，用于帮助内容提供者匹配URIs
    private static UriMatcher uriMatcher;

    //数据库帮助类
    private DataBaseHelper dbHelper;

    // 添加路径匹配规则
    static {
        // 添加一个path(匹配规则)，如果匹配成功会返回code值。
        // authority：主机名（我们可以自己定义，随意写，当使用内容解析者时会使用到）
        // path：路径，一般是操作名称
        // code：响应码
        uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
        uriMatcher.addURI(AUTHORITY, DataBaseHelper.TABLE_BOOK_NAME, BOOK_DIR);
        uriMatcher.addURI(AUTHORITY, DataBaseHelper.TABLE_BOOK_NAME + "/#", BOOK_ITEM);
        uriMatcher.addURI(AUTHORITY, DataBaseHelper.TABLE_CATEGORY_NAME, CATEGORY_DIR);
        uriMatcher.addURI(AUTHORITY, DataBaseHelper.TABLE_CATEGORY_NAME + "/#", CATEGORY_ITEM);
    }

    public MyBookProvider() {
    }

    @Override
    public boolean onCreate() {
        dbHelper = new DataBaseHelper(getContext(), DATABASE_NAME, null, DATABASE_VERSION);
        return true;
    }


    @Override
    public Cursor query(Uri uri, String[] projection, String selection,
                        String[] selectionArgs, String sortOrder) {
        //查询数据库
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        Cursor cursor = null;
        switch (uriMatcher.match(uri)) {
            case BOOK_DIR:
                cursor = db.query(DataBaseHelper.TABLE_BOOK_NAME, projection, selection, selectionArgs, null, null, sortOrder);
                break;
            case BOOK_ITEM:
                //这个方法会将URI权限之后的部分已"/"符号进行分割,并把分割后的结果放入到一个字符串列表中,第0个位置是路径,第1个位置是id
                String bookId = uri.getPathSegments().get(1);
                cursor = db.query(DataBaseHelper.TABLE_BOOK_NAME, projection, "id = ?", new String[]{bookId}, null, null, sortOrder);
                break;
            case CATEGORY_DIR:
                cursor = db.query(DataBaseHelper.TABLE_CATEGORY_NAME, projection, selection, selectionArgs, null, null, sortOrder);
                break;
            case CATEGORY_ITEM:
                String categoryId = uri.getPathSegments().get(1);
                cursor = db.query(DataBaseHelper.TABLE_CATEGORY_NAME, projection, "id = ?", new String[]{categoryId}, null, null, sortOrder);
                break;
            default:
                break;
        }
        return cursor;
    }

    @Override
    public Uri insert(Uri uri, ContentValues values) {
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        Uri uriReturn = null;
        switch (uriMatcher.match(uri)) {
            case BOOK_DIR:
            case BOOK_ITEM:
                long newBookId = db.insert(DataBaseHelper.TABLE_BOOK_NAME, null, values);
                uri = uri.parse("content://" + AUTHORITY + "/" + DataBaseHelper.TABLE_BOOK_NAME + "/" + newBookId);
                break;
            case CATEGORY_DIR:
            case CATEGORY_ITEM:
                long newCategoryId = db.insert(DataBaseHelper.TABLE_CATEGORY_NAME, null, values);
                uri.parse("content://" + AUTHORITY + "/" + DataBaseHelper.TABLE_CATEGORY_NAME + "/" + newCategoryId);
                break;
            default:
                break;
        }
        return uri;
    }

    @Override
    public int update(Uri uri, ContentValues values, String selection,
                      String[] selectionArgs) {
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        int updateRows = 0;
        switch (uriMatcher.match(uri)) {
            case BOOK_DIR:
                updateRows = db.update(DataBaseHelper.TABLE_BOOK_NAME, values, selection, selectionArgs);
                break;
            case BOOK_ITEM:
                String bookId = uri.getPathSegments().get(1);
                updateRows = db.update(DataBaseHelper.TABLE_BOOK_NAME, values, "id = ?", new String[]{bookId});
                break;
            case CATEGORY_DIR:
                updateRows = db.update(DataBaseHelper.TABLE_CATEGORY_NAME, values, selection, selectionArgs);
                break;
            case CATEGORY_ITEM:
                String categoryId = uri.getPathSegments().get(1);
                updateRows = db.update(DataBaseHelper.TABLE_CATEGORY_NAME, values, "id = ?", new String[]{categoryId});
                break;
            default:
                break;
        }
        return updateRows;
    }

    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        int deletedRows = 0;
        switch (uriMatcher.match(uri)) {
            case BOOK_DIR:
                deletedRows = db.delete(DataBaseHelper.TABLE_BOOK_NAME, selection, selectionArgs);
                break;
            case BOOK_ITEM:
                String bookId = uri.getPathSegments().get(1);
                deletedRows = db.delete(DataBaseHelper.TABLE_BOOK_NAME, "id = ?", new String[]{bookId});
                break;
            case CATEGORY_DIR:
                deletedRows = db.delete(DataBaseHelper.TABLE_CATEGORY_NAME, selection, selectionArgs);
                break;
            case CATEGORY_ITEM:
                String categoryId = uri.getPathSegments().get(1);
                deletedRows = db.delete(DataBaseHelper.TABLE_CATEGORY_NAME, "id = ?", new String[]{categoryId});
                break;
            default:
                break;
        }
        return deletedRows;
    }


    /**
     * 获得uri索对象的MIME类型
     * @param uri
     * @return
     */
    @Override
    public String getType(Uri uri) {
        switch (uriMatcher.match(uri)) {
            case BOOK_DIR:
                return "vnd.android.cursor.dir/vnd." + AUTHORITY + "." + DataBaseHelper.TABLE_BOOK_NAME;
            case BOOK_ITEM:
                return "vnd.android.cursor.item/vnd." + AUTHORITY + "." + DataBaseHelper.TABLE_BOOK_NAME;
            case CATEGORY_DIR:
                return "vnd.android.cursor.dir/vnd." + AUTHORITY + "." + DataBaseHelper.TABLE_CATEGORY_NAME;
            case CATEGORY_ITEM:
                return "vnd.android.cursor.item/vnd." + AUTHORITY + "." + DataBaseHelper.TABLE_CATEGORY_NAME;
            default:
                break;
        }
        return null;
    }
}
